package com.kuanter.kuanterauto.view;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.entity.CardInfo;
import com.kuanter.kuanterauto.utils.Utility;

public class SelecterPopupWindow extends PopupWindow {


	private TextView btn_cancel;
	private View mMenuView;
	
	ListView insuranceview;
	LayoutInflater inflater;
	
	PopInsuranceAdapter adapter;
	
	List<CardInfo> ci_pop;

	public SelecterPopupWindow(Activity context,OnItemClickListener itemsOnClick,List<CardInfo> ci_pop) {
		super(context);
		this.ci_pop = ci_pop;
		
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.alert_dialog, null);
		btn_cancel = (TextView) mMenuView.findViewById(R.id.btn_cancel);
		//取消按钮
		btn_cancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				//销毁弹出框
				dismiss();
			}
		});
		
		insuranceview = (ListView) mMenuView.findViewById(R.id.insuranceview);
		insuranceview.setOnItemClickListener(itemsOnClick);
		
		adapter = new PopInsuranceAdapter();
		insuranceview.setAdapter(adapter);
		Utility.setListViewHeightBasedOnChildren(insuranceview);
		
		//设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		//设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.FILL_PARENT);
		//设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		//设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		//设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		//实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		//设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				
				int height = mMenuView.findViewById(R.id.pop_layout).getTop();
				int y=(int) event.getY();
				if(event.getAction()==MotionEvent.ACTION_UP){
					if(y<height){
						dismiss();
					}
				}				
				return true;
			}
		});

	}
	
	class PopInsuranceAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return ci_pop == null ? 0 : ci_pop.size();
		}

		@Override
		public Object getItem(int arg0) {
			return ci_pop.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if(view == null) {
				view = inflater.inflate(R.layout.pop_insurance_item, null);
			}
			TextView item_insurance = (TextView) view.findViewById(R.id.item_insurance);
			item_insurance.setText(ci_pop.get(position).getCardNumber());
			return view;
		}
		
	}

}
